#parse('include.java')
#set($sp = $CSU.getSuperPackage($model.package,1))
#set($TYPE = $model.name)
#set($type = $CSU.getVariable($TYPE))
#set($serviceName = $type+'Service')
#if($sp)
package ${sp}.action;
#end
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import $!{model.package}.${TYPE};
import ${sp}.service.${TYPE}Service;
import ${sp}.action.BasicBizAction;

/**
 * $!{modelLabel}操作接口(json)
 * @author wangjg
 */
@Controller
public class ${TYPE}JsonAction extends BasicBizAction{
	@Autowired
	private ${TYPE}Service ${serviceName};

	/**
	 * $!{modelLabel}列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/${type}/list.json")
	public String list(#allParams($model, true)){
		Page page = this.getPage(20);
		List<${TYPE}> list = ${type}Service.pageQuery(page#allParamVals($model));
		return this.getPageResult(page, list);
	}

	/**
	 * 获取$!{modelLabel}
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/${type}/get.json")
	public String get($IdType id){
		${TYPE} ${type} = ${serviceName}.get${TYPE}(id);
		this.checkOwner(${type});
		return this.getSuccessResult(${type});
	}

	/**
	 * 保存$!{modelLabel}
	 */
	@RequestMapping("/${type}/save.json")
	public String save($IdType id){
		if(id==null){
			${TYPE} ${type} = new ${TYPE}();
			this.bindParams(${type});
			this.setOwner(${type});
			this.${type}Service.save(${type});
		}else{
			${TYPE} ${type} = this.${type}Service.get${TYPE}(id);
			this.checkOwner(${type});
			this.bindParams(${type});
			this.${type}Service.update(${type});
		}
		return this.getSuccessResult();
	}

	/**
	 * 启用/禁用$!{modelLabel}
	 * @param id 编号
	 * @param enabled 是否启用
	 * @return
	 */
	@RequestMapping("/${type}/enable.json")
	public String enable(Long id, String enabled){
		if(!"Y".equals(enabled)){
			enabled = "N";
		}
		${TYPE} ${type} = ${type}Service.get${TYPE}(id);
		this.checkOwner(${type});
		${type}.setEnabled(enabled);
		this.${type}Service.update(${type});
		return this.getSuccessResult(enabled);
	}

	/**
	 * 删除$!{modelLabel}
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/${type}/delete.json")
	public String delete($IdType id){
		${TYPE} ${type} = ${serviceName}.get${TYPE}(id);
		this.checkOwner(${type});
		this.${serviceName}.delete(${type});
		return this.getSuccessResult();
	}

	/**
	 * 批量删除$!{modelLabel}
	 * @param idseq 编号序列(以‘,’号隔开)
	 * @return
	 */
	@RequestMapping("/${type}/batchDelete.json")
	public String batchDelete(String idseq){
		this.${type}Service.batchDelete(idseq);
		return this.getSuccessResult();
	}

	/**
	 * 获取$!{modelLabel}列表
	 * @param keyword
	 * @return
	 */
	@RequestMapping("/${type}/select.json")
	public String select(#allParams($model, true)){
		List<${TYPE}> list = ${serviceName}.pageQuery(null#allParamVals($model));
		return this.getSuccessResult(list);
	}

#set($mtmList = $MU.getModelMtmList($model))
#foreach($mtm in $mtmList)
	#set($RelationItem = $CSU.getSimpleType($mtm.relation))
	#set($Model2 = $CSU.getSimpleType($mtm.anotherModel))

	/**
	 * 保存关系
	 * @param $mtm.relationKey
	 * @param $mtm.anotherRelationKey 
	 * @return
	 */
	@RequestMapping("/${type}/setting/save${Model2}s.json")
	public String save${Model2}s(Long ${mtm.relationKey}, String ${mtm.anotherRelationKey}) {
		${TYPE} ${type} = this.${type}Service.get${TYPE}(${mtm.relationKey});
		this.checkOwner(${type});
		${serviceName}.save${Model2}s(${mtm.relationKey}, ${mtm.anotherRelationKey});
		return this.getSuccessResult();
	}

	/**
	 * 获取$!{modelLabel}关联的 id列表
	 * @param  $mtm.relationKey
	 * @return
	 */
	@RequestMapping("/${type}/setting/get${Model2}IdList.json")
	public String get${Model2}IdList(Long ${mtm.relationKey}) {
		${TYPE} ${type} = this.${type}Service.get${TYPE}(${mtm.relationKey});
		this.checkOwner(${type});
		List<Long> list = ${serviceName}.get${Model2}IdList(${mtm.relationKey});
		return this.getSuccessResult(list);
	}

#end

}
